<?php

namespace app\admin\controller\node;

use app\common\controller\Backend;
use think\Db;

/**
 * 节点服务流量统计
 *
 * @icon fa fa-circle-o
 */
class Nodeflow extends Backend
{
    
    /**
     * Nodeflow模型对象
     * @var \app\admin\model\node\Nodeflow
     */
    protected $model = null;

    public function initialize()
    {
        parent::initialize();
        $this->model = new \app\admin\model\node\Nodeflow;

    }
    
    /**
     * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
     * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
     * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
     */
    

    /**
     * 查看
     */
    public function index()
    {
        //当前是否为关联查询
        $this->relationSearch = false;
        //设置过滤方法
        $this->request->filter(['strip_tags']);
        if ($this->request->isAjax())
        {
            //如果发送的来源是Selectpage，则转发到Selectpage
            if ($this->request->request('keyField'))
            {
                return $this->selectpage();
            }
            // list($where, $sort, $order, $offset, $limit) = $this->buildparams();
            // $total = $this->model
                    
            //         ->where($where)
            //         ->order($sort, $order)
            //         ->count();
            // $starttime = date("Y-m-d H:i:00",strtotime("-2 hour",time()));
            // $endtime = date("Y-m-d H:i:00",time());
            $where = [];
            if(input('typename')){
                $typename = input('typename');
                if($typename == 1){
                    $status = input('compare');
                    $res = DB::name('node_attr')->where(['ab_tongji'=>$status])->select();
                    $where_in = [];
                    foreach ($res as $key => $value) {
                        $where_in[] = $value['ip'];
                    }
                    $where['ip'] = $where_in;
                    if(input('compare') == 1){
                        $name_str = 'A点统计';
                    }else{
                        $name_str = 'B点统计'; 
                    }
                }elseif ($typename == 2) {
                    $status = input('compare');
                    $res = DB::name('node_attr')->where(['area'=>$status])->select();
                    $where_in = [];
                    foreach ($res as $key => $value) {
                        $where_in[] = $value['ip'];
                    }
                    $where['ip'] = $where_in;
                    if(input('compare') == 1){
                        $name_str = '国内节点统计';
                    }else{
                        $name_str = '国外节点统计'; 
                    }
                }
            }else{
                // $field_actual = "IF(bandwidthdetail.balance_id=21,sum(bandwidthdetail.prepaid_payment),sum(bandwidthdetail.recheck)) as total_amount,bandwidth.bandwidthname as name";
                // $group_actual = "bandwidth.bandwidthname";
                // $field_budget = "sum(totalprice) as total_amount,bandwidth.bandwidthname as name,programbudget.version_id as version_id";
                // $group_budget = "bandwidth.bandwidthname,programbudget.version_id";
                // $name_str = 'A点';
                // echo "test";die;
            }
            if(input('ip')){
                $where['ip'] = input("ip");
                $res = DB::name('node_attr')->where($where)->find();
                $ab_tongji = ['1'=>'A点','2'=>'B点'];
                $area = ['1'=>'国内节点','2'=>'国外节点'];
                if(empty($ab_tongji[$res['ab_tongji']])){
                    $ab_tongji[$res['ab_tongji']] = '';
                }
                if(empty($ab_tongji[$res['ab_tongji']])){
                    $area[$res['area']] = '';
                }
                $name_str =  '服务器类型:' . $ab_tongji[$res['ab_tongji']] . ' 部署位置:' .$area[$res['area']] . ' IP地址:' . $res['ip'];
            }
            // $where = [];
            $where_time = [];
            $starttime = input('starttime');
            $endtime = input('endtime');
            $text_title = '';
            if(input('starttime') && input('endtime')){
                $date = [$starttime,$endtime];
                $where_time[] = ['time', "BETWEEN", $date];
            }else{
                if($starttime || $endtime){
                    if ($starttime == '') {
                        $sym = "<=";
                        $arr = $endtime;
                    } else if ($endtime == '') {
                        $sym = ">=";
                        $arr = $starttime;
                    }
                    $where_time[] = ['time', $sym, $arr];
                }
            }
            $res_list = $this->model
                    ->field("ip,sum(flow_rx) as flow_rx,sum(flow_tx) as flow_tx,sum(tcp_connection_num) as tcp_connection_num,time")
                    ->where($where_time)
                    ->where($where)
                    ->group('time')
                    ->order("time asc")
                    ->select();
            // echo $this->model->getlastsql();die;
            // dump($list);die;
            $list =array(
                "series" => [
                    ["data" => [0], "name" => '入流量'],
                    ["data" => [0], "name" => '出流量']
                ],
                "xAxis" => [],
                "name" => $name_str,
                "id" => "33333333333333333",
            );
            $time = [];
            $InSpeed = [];   //入
            $OutSpeed = [];  //出
            foreach ($res_list as $k => $v) {
                $time[] = $v['time'];
                $InSpeed[] = (int)$v['flow_rx'];
                $OutSpeed[] = (int)$v['flow_tx'];

            }
            $list['xAxis'] = $time;
            $list['series'][0]['data'] = $InSpeed;
            $list['series'][1]['data'] = $OutSpeed;
            // $list = $list->toArray();
            // $result = array("total" => $total, "rows" => $list);
            return json($list);
        }
        $ip_res = DB::name('node_attr')->select();
        $start_time = date("Y-m-d H:i:00",strtotime("-1 hour",time()));
        $end_time = date("Y-m-d H:i:00",time());
        $this->view->assign("start_time", $start_time);
        $this->view->assign("end_time", $end_time);
        $this->view->assign("ip_res", $ip_res);
        return $this->view->fetch();
    }
}
